/* flightReservation package contains classes that enable
 * users to make reservations with a particular airline
 */
package ReservationGUI;

import flightReservation.*;
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.CardLayout;
//Java core packages
//import java.awt.*;
import java.awt.event.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
// Java extension packages
import javax.swing.*;


import javax.swing.border.LineBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.text.MaskFormatter;
import javax.swing.table.DefaultTableModel;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Toolkit;
import java.util.regex.Matcher;  
import java.util.regex.Pattern;
import java.awt.Dimension;
import java.io.IOException;
import java.io.OptionalDataException;


public class ReservationGUI extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//private JPanel contentPane;
	JPanel cardPanel;
	private JTextField textCustomerName;
	private JTextField textEmailId;
	private JTextField textAddress;
	JFormattedTextField formattedTexttextContactNo ;
		
	private JTable tableReservation;
	private JTable tableWaiting;
	private JTextField textEmailId_find;
	private JTextField textEmailId_res;
	private JTextField textReservationNo;
	private JTextField textReservationNo_res;
	private JTable tableFindPassenger;
	private JTable tableFindReservation;
	
	JPanel seatPanel;
	GridBagLayout gbl_seatPanel ;
	JButton [][] buttonArray;
	JButton btn;
	JButton btnResereve ;
	JButton btnAddWaiting;
	JButton btnClear;
	JButton btnPrintReservation;
	JButton btnCancelReservation ;
	JLabel lblNoOfSeats ;
	JLabel lblSeatNo ;
	
	Passenger objPassenger;
	Seat objSeat;
	Reservation objRes;
	Flight objFlight;
	String seatNo;
	String btnName;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		
						try {
					ReservationGUI frame = new ReservationGUI();
					frame.setVisible(true);
					frame.setDefaultCloseOperation(
					         JFrame.EXIT_ON_CLOSE );
				} catch (Exception e) {
					e.printStackTrace();
				
		}
	}

	/**
	 * Create the frame.
	 */
	public ReservationGUI() {
		setResizable(false);
		setTitle("Airline Reservation system");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 857, 500);
		
		 Toolkit tk = Toolkit.getDefaultToolkit();
	    Dimension screenSize = tk.getScreenSize();
	    int screenHeight = screenSize.height;
	    int screenWidth = screenSize.width;
	    //setSize(screenWidth *2/3 , screenHeight*2/3 );
	    setLocation(screenWidth / 5, screenHeight / 5);
		
		try {
			objFlight = Flight.deserializeFlight();
			objSeat = null;
			objRes = null;
			objPassenger = null;
		} catch (OptionalDataException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if(objFlight == null)
		{
			objFlight = new Flight("636", "SFO", "JFK", new Date(), new Date());
		}
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnReservation = new JMenu("Reservation");
		mnReservation.setFont(new Font("Segoe UI", Font.BOLD, 13));
		menuBar.add(mnReservation);
		
		//menu "make reservation" and event handler
		JMenuItem mntmMakeReservation = new JMenuItem("Make Reservation");
		mntmMakeReservation.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, InputEvent.ALT_MASK));
		mntmMakeReservation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showHidePanel("Make Reservation");
				textCustomerName.requestFocusInWindow();
				showSeatPanelDetails();
			}
		});
		mnReservation.add(mntmMakeReservation);
		
		//menu "Find Passenger" and event handler
		JMenuItem mntmFindPassenger = new JMenuItem("Find Passenger");
		mntmFindPassenger.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.ALT_MASK));
		mntmFindPassenger.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showHidePanel("Find Passenger");
			}
		});
		mnReservation.add(mntmFindPassenger);
		
		//menu "View Passenger List" and event handler
		JMenuItem mntmViewPassengerList = new JMenuItem("View Passenger List");
		mntmViewPassengerList.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.ALT_MASK));
		mntmViewPassengerList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showHidePanel("View Passenger List");
				ViewPassengerList();
			}
		});
		mnReservation.add(mntmViewPassengerList);
		
		//menu "Cancel reservation" and event handler
		JMenuItem mntmCancelReservation = new JMenuItem("Cancel Reservation");
		mntmCancelReservation.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.ALT_MASK));
		mntmCancelReservation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showHidePanel("Find Reservation");
				btnCancelReservation.setVisible(true);
				btnPrintReservation.setVisible(false);
				setControls();
			}
		});
		mnReservation.add(mntmCancelReservation);
		
		
		//menu "Print reservation" and event handler
		JMenuItem mntmPrintReservation = new JMenuItem("Print Reservation");
		mntmPrintReservation.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.ALT_MASK));
		mntmPrintReservation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showHidePanel("Find Reservation");
				btnPrintReservation.setVisible(true);
				btnCancelReservation.setVisible(false);
				setControls();
			}
		});
		mnReservation.add(mntmPrintReservation);
		
		//menu "Print reservation" and event handler
		JMenuItem mntmViewSeatsAvailable = new JMenuItem("View Available Seats");
		mntmViewSeatsAvailable.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.ALT_MASK));
		mntmViewSeatsAvailable.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showHidePanel("View Available Seats");
				viewAvailableSeats();
			}
		});
		mnReservation.add(mntmViewSeatsAvailable);
				
		cardPanel = new JPanel();
		cardPanel.setBounds(0, 0, 841, 441);
		getContentPane().add(cardPanel);
		cardPanel.setLayout(new CardLayout(0, 0));
		

		
		JPanel homePanel = new JPanel();
		cardPanel.add(homePanel, "Home");
		homePanel.setLayout(null);
		
		JLabel lblWelcomeToGlobetrotters = new JLabel("Welcome to GlobeTrotters Airlines");
		lblWelcomeToGlobetrotters.setHorizontalAlignment(SwingConstants.CENTER);
		lblWelcomeToGlobetrotters.setBounds(0, 5, 841, 22);
		lblWelcomeToGlobetrotters.setFont(new Font("Tahoma", Font.BOLD, 20));
		homePanel.add(lblWelcomeToGlobetrotters);
		
		JButton btnMakeReservation = new JButton("Make Reservation");
		btnMakeReservation.setBounds(319, 178, 176, 23);
		btnMakeReservation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showHidePanel("Make Reservation");
				textCustomerName.requestFocusInWindow();
				showSeatPanelDetails();
			}
		});
		btnMakeReservation.setFont(new Font("Tahoma", Font.BOLD, 12));
		homePanel.add(btnMakeReservation);
		
		JLabel lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setBackground(Color.WHITE);
		lblNewLabel_1.setIcon(new ImageIcon("C:\\Users\\Priyanka\\Pictures\\logo_globe.gif"));
		lblNewLabel_1.setBounds(26, 5, 221, 148);
		homePanel.add(lblNewLabel_1);
		
		JPanel makeReservationPanel = new JPanel();
		makeReservationPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		cardPanel.add(makeReservationPanel, "Make Reservation");
		makeReservationPanel.setLayout(new BorderLayout(0, 0));
		
		JPanel titlePanel = new JPanel();
		titlePanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		FlowLayout flowLayout_1 = (FlowLayout) titlePanel.getLayout();
		flowLayout_1.setVgap(6);
		makeReservationPanel.add(titlePanel, BorderLayout.NORTH);
		
		JLabel lblMakeReservation = new JLabel("Make Reservation");
		lblMakeReservation.setFont(new Font("Tahoma", Font.BOLD, 16));
		titlePanel.add(lblMakeReservation);
		
		JPanel flightPanel = new JPanel();
		flightPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		makeReservationPanel.add(flightPanel, BorderLayout.WEST);
		flightPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,},
			new RowSpec[] {
				FormFactory.LINE_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.LINE_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.LINE_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.LINE_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.LINE_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.LINE_GAP_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		
		JLabel lblFlightNo = new JLabel("Flight No. ");
		lblFlightNo.setFont(new Font("Tahoma", Font.BOLD, 13));
		flightPanel.add(lblFlightNo, "2, 2, fill, fill");
		
		JLabel lblFlightNoVal = new JLabel(objFlight.getFlightNumber());
		lblFlightNoVal.setFont(new Font("Tahoma", Font.PLAIN, 12));
		flightPanel.add(lblFlightNoVal, "4, 2, fill, fill");
		
		JLabel lblFrom = new JLabel("From");
		lblFrom.setFont(new Font("Tahoma", Font.BOLD, 13));
		flightPanel.add(lblFrom, "2, 4, fill, fill");
		
		JLabel lblFromVal = new JLabel(objFlight.getOrigin());
		lblFromVal.setFont(new Font("Tahoma", Font.PLAIN, 12));
		flightPanel.add(lblFromVal, "4, 4, fill, fill");
		
		JLabel lblDeparting = new JLabel("Departing");
		lblDeparting.setFont(new Font("Tahoma", Font.BOLD, 13));
		flightPanel.add(lblDeparting, "2, 6, fill, fill");
		
		JLabel lblDepartingVal = new JLabel("09/22/2011  8.00 AM");
		lblDepartingVal.setFont(new Font("Tahoma", Font.PLAIN, 12));
		flightPanel.add(lblDepartingVal, "4, 6, fill, fill");
		
		JLabel lblTo = new JLabel("To");
		lblTo.setFont(new Font("Tahoma", Font.BOLD, 13));
		flightPanel.add(lblTo, "2, 8, fill, fill");
		
		JLabel lblJfk = new JLabel(objFlight.getDestination());
		lblJfk.setFont(new Font("Tahoma", Font.PLAIN, 12));
		flightPanel.add(lblJfk, "4, 8, fill, fill");
		
		JLabel lblArriving = new JLabel("Arriving");
		lblArriving.setFont(new Font("Tahoma", Font.BOLD, 13));
		flightPanel.add(lblArriving, "2, 10, fill, fill");
		
		JLabel lblArrivingVal = new JLabel("09/22/2011  3.30 PM");
		lblArrivingVal.setFont(new Font("Tahoma", Font.PLAIN, 13));
		flightPanel.add(lblArrivingVal, "4, 10, fill, fill");
					
		JPanel customerPanel = new JPanel();
		customerPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		makeReservationPanel.add(customerPanel, BorderLayout.CENTER);
		customerPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.UNRELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,},
			new RowSpec[] {
				FormFactory.LINE_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.LINE_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.LINE_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.LINE_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.LINE_GAP_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblCustomerName = new JLabel("Customer Name");
		lblCustomerName.setHorizontalAlignment(SwingConstants.LEFT);
		lblCustomerName.setFont(new Font("Tahoma", Font.BOLD, 13));
		customerPanel.add(lblCustomerName, "2, 2, left, fill");
		
		textCustomerName = new JTextField();
		textCustomerName.setFont(new Font("Tahoma", Font.PLAIN, 13));
		customerPanel.add(textCustomerName, "4, 2, 2, 1, fill, fill");
		textCustomerName.setColumns(15);
		
		JLabel lblEmailId = new JLabel("Email Id");
		lblEmailId.setHorizontalAlignment(SwingConstants.LEFT);
		lblEmailId.setFont(new Font("Tahoma", Font.BOLD, 13));
		customerPanel.add(lblEmailId, "2, 4, fill, fill");
		
		textEmailId = new JTextField();
		textEmailId.setFont(new Font("Tahoma", Font.PLAIN, 13));
		customerPanel.add(textEmailId, "4, 4, 2, 1, fill, fill");
		textEmailId.setColumns(15);
		
		JLabel lblAddress = new JLabel("Address");
		lblAddress.setHorizontalAlignment(SwingConstants.TRAILING);
		lblAddress.setFont(new Font("Tahoma", Font.BOLD, 13));
		customerPanel.add(lblAddress, "2, 6, left, fill");
		
		textAddress = new JTextField();
		textAddress.setFont(new Font("Tahoma", Font.PLAIN, 13));
		customerPanel.add(textAddress, "4, 6, fill, fill");
		textAddress.setColumns(15);
		
		JLabel lblContactNo = new JLabel("Contact No. ");
		lblContactNo.setFont(new Font("Tahoma", Font.BOLD, 13));
		customerPanel.add(lblContactNo, "2, 8, fill, fill");
		
		MaskFormatter mf2;
		try {
			mf2 = new MaskFormatter("(###) ###-####");
			formattedTexttextContactNo = new JFormattedTextField(mf2);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		formattedTexttextContactNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
		customerPanel.add(formattedTexttextContactNo, "4, 8, fill, fill");
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		FlowLayout flowLayout = (FlowLayout) buttonPanel.getLayout();
		flowLayout.setVgap(10);
		flowLayout.setHgap(10);
		makeReservationPanel.add(buttonPanel, BorderLayout.SOUTH);
		
		btnResereve = new JButton("Reserve");
		btnResereve.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			makeReservationForPassenger();
			}
		});
		btnResereve.setFont(new Font("Tahoma", Font.BOLD, 11));
		buttonPanel.add(btnResereve);
		
		btnAddWaiting = new JButton("Add To Waiting List");
		btnAddWaiting.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			addPassengerToWaitingList();
			}
		});
		btnAddWaiting.setFont(new Font("Tahoma", Font.BOLD, 11));
		buttonPanel.add(btnAddWaiting);
		
		btnClear= new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			clearText();
			
			}
		});
		btnClear.setFont(new Font("Tahoma", Font.BOLD, 11));
		buttonPanel.add(btnClear);
		
		seatPanel = new JPanel();
		seatPanel.setAlignmentX(10.0f);
		seatPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		makeReservationPanel.add(seatPanel, BorderLayout.EAST);
		gbl_seatPanel = new GridBagLayout();
		/*gbl_seatPanel.columnWidths = new int[]{45, 45, 45, 45, 45};
		gbl_seatPanel.rowHeights = new int[]{23, 0, 0};
		gbl_seatPanel.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_seatPanel.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};*/
		seatPanel.setLayout(gbl_seatPanel);
		
		buttonArray = new JButton[objFlight.getMaxrows()][objFlight.getMaxSeatPerRow()];
		
		for (int idx = 0; idx < objFlight.getMaxrows(); idx++)
		{
			for (int idy = 0; idy < objFlight.getMaxSeatPerRow(); idy++)
			{
				btnName = Integer.toString(idx+1) + Flight.cols.charAt(idy);
				btn = new JButton(btnName);
				buttonArray[idx][idy] = btn;
				btn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						setSelectedSeatNo(arg0.getActionCommand());
						showSeatPanelDetails();
						((JButton)arg0.getSource()).setBackground(Color.blue);
					}
				});
				
				//btn.setBounds(0, 0, 135, 23);
				GridBagConstraints gbc_btn1A = new GridBagConstraints();
				gbc_btn1A.anchor = GridBagConstraints.NORTHWEST;
				//gbc_btn1A.insets = new Insets(0, 0, 5, 5);
				gbc_btn1A.gridx = idy;
				gbc_btn1A.gridy = idx;
				seatPanel.add(btn, gbc_btn1A);
			}
		}
					
		JPanel viewPassengerListPanel = new JPanel();
		viewPassengerListPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
		cardPanel.add(viewPassengerListPanel, "View Passenger List");
		viewPassengerListPanel.setLayout(null);
		
		JLabel lblInfo = new JLabel("Flight 636 From SFO to JFK");
		lblInfo.setBounds(1, 1, 840, 29);
		lblInfo.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblInfo.setHorizontalAlignment(SwingConstants.CENTER);
		viewPassengerListPanel.add(lblInfo);
				
		JLabel lblViewReservationList = new JLabel("Reservation List");
		lblViewReservationList.setBounds(1, 27, 840, 43);
		lblViewReservationList.setHorizontalAlignment(SwingConstants.CENTER);
		lblViewReservationList.setFont(new Font("Tahoma", Font.BOLD, 14));
		viewPassengerListPanel.add(lblViewReservationList);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(1, 81, 840, 146);
		scrollPane.setViewportBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		viewPassengerListPanel.add(scrollPane);
		
		tableReservation = new JTable();
		tableReservation.setEnabled(false);
		tableReservation.setFont(new Font("Tahoma", Font.PLAIN, 14));
		scrollPane.setViewportView(tableReservation);
		tableReservation.setBorder(new LineBorder(new Color(0, 0, 0)));
		tableReservation.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Seat No", "Email Id", "Passenger Name", "Reservation No"
			}
		) {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			boolean[] columnEditables = new boolean[] {
				false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		
		tableReservation.getTableHeader().setFont(new Font("Tahoma", Font.BOLD, 13));
		
		JLabel lblNewLabel = new JLabel("Waiting List");
		lblNewLabel.setBounds(1, 227, 840, 29);
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		viewPassengerListPanel.add(lblNewLabel);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(1, 267, 840, 134);
		viewPassengerListPanel.add(scrollPane_1);
		
		tableWaiting = new JTable();
		tableWaiting.setFont(new Font("Tahoma", Font.PLAIN, 14));
		scrollPane_1.setViewportView(tableWaiting);
		tableWaiting.setBorder(new LineBorder(new Color(0, 0, 0)));
		tableWaiting.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Passenger Name", "Email Id"
			}
		));
		
		tableWaiting.getTableHeader().setFont(new Font("Tahoma", Font.BOLD, 13));
		
		JPanel findPassengerPanel = new JPanel();
		findPassengerPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		cardPanel.add(findPassengerPanel, "Find Passenger");
		findPassengerPanel.setLayout(null);
		
		JLabel lblFindPassenger = new JLabel("Find Passenger");
		lblFindPassenger.setBounds(2, 2, 839, 28);
		lblFindPassenger.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblFindPassenger.setHorizontalAlignment(SwingConstants.CENTER);
		findPassengerPanel.add(lblFindPassenger);
		
		JPanel panelSearch = new JPanel();
		panelSearch.setBounds(2, 32, 839, 82);
		findPassengerPanel.add(panelSearch);
		panelSearch.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.PREF_COLSPEC,
				ColumnSpec.decode("16dlu"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.LINE_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.PARAGRAPH_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.PARAGRAPH_GAP_ROWSPEC,}));
		
		JLabel lblEmailId_1 = new JLabel("Email Id");
		lblEmailId_1.setFont(new Font("Tahoma", Font.BOLD, 13));
		panelSearch.add(lblEmailId_1, "2, 2, left, default");
		
		textEmailId_find = new JTextField();
		textEmailId_find.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panelSearch.add(textEmailId_find, "4, 2, left, default");
		textEmailId_find.setColumns(20);
		
		JLabel lblOr = new JLabel("OR");
		lblOr.setFont(new Font("Tahoma", Font.BOLD, 13));
		panelSearch.add(lblOr, "7, 2");
		
		JLabel lblReservationno = new JLabel("Reservation No");
		lblReservationno.setFont(new Font("Tahoma", Font.BOLD, 13));
		panelSearch.add(lblReservationno, "9, 2, left, default");
		
		textReservationNo = new JTextField();
		textReservationNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panelSearch.add(textReservationNo, "12, 2, left, default");
		textReservationNo.setColumns(20);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				FindPassenger();
			}
		});
		panelSearch.add(btnSearch, "12, 4, center, default");
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(2, 125, 839, 135);
		findPassengerPanel.add(scrollPane_2);
		
		tableFindPassenger = new JTable();
		tableFindPassenger.setFont(new Font("Tahoma", Font.PLAIN, 14));
		scrollPane_2.setViewportView(tableFindPassenger);
		tableFindPassenger.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Passenger Name", "Email Id", "Seat No", "Reservation No", "Status"
			}
		) {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			boolean[] columnEditables = new boolean[] {
				false, false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		tableFindPassenger.getTableHeader().setFont(new Font("Tahoma", Font.BOLD, 13));
		
		JPanel findReservationPanel = new JPanel();
		findReservationPanel.setSize(new Dimension(1, 1));
		cardPanel.add(findReservationPanel, "Find Reservation");
		findReservationPanel.setLayout(null);
		
		JLabel lblFindReservation = new JLabel("Find Reservation");
		lblFindReservation.setBounds(2, 0, 839, 30);
		lblFindReservation.setHorizontalAlignment(SwingConstants.CENTER);
		lblFindReservation.setFont(new Font("Tahoma", Font.BOLD, 15));
		findReservationPanel.add(lblFindReservation);
		
		JPanel panelSearch1 = new JPanel();
		panelSearch1.setBounds(2, 32, 839, 82);
		findReservationPanel.add(panelSearch1);
		panelSearch1.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.PREF_COLSPEC,
				ColumnSpec.decode("16dlu"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.LINE_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.PARAGRAPH_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.PARAGRAPH_GAP_ROWSPEC,}));
		
		JLabel lblEmailId_res = new JLabel("Email Id");
		lblEmailId_res.setFont(new Font("Tahoma", Font.BOLD, 13));
		panelSearch1.add(lblEmailId_res, "2, 2, left, default");
		
		textEmailId_res = new JTextField();
		textEmailId_res.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panelSearch1.add(textEmailId_res, "4, 2, left, default");
		textEmailId_res.setColumns(20);
		
		JLabel lblOr1 = new JLabel("OR");
		lblOr1.setFont(new Font("Tahoma", Font.BOLD, 13));
		panelSearch1.add(lblOr1, "7, 2");
		
		JLabel lblReservationno1 = new JLabel("Reservation No");
		lblReservationno1.setFont(new Font("Tahoma", Font.BOLD, 13));
		panelSearch1.add(lblReservationno1, "9, 2, left, default");
		
		textReservationNo_res = new JTextField();
		textReservationNo_res.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panelSearch1.add(textReservationNo_res, "12, 2, left, default");
		textReservationNo_res.setColumns(20);
		
		JButton btnSearch1 = new JButton("Search");
		btnSearch1.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnSearch1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				FindReservation();
			}
		});
		panelSearch1.add(btnSearch1, "12, 4, center, default");
		
		
		JScrollPane scrollPane_3 = new JScrollPane();
		scrollPane_3.setBounds(2, 125, 839, 135);
		findReservationPanel.add(scrollPane_3);
		
		tableFindReservation = new JTable();
		tableFindReservation.setFont(new Font("Tahoma", Font.PLAIN, 14));
		scrollPane_3.setViewportView(tableFindReservation);
		tableFindReservation.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Passenger Name", "Email Id", "Seat No", "Reservation No"
			}
		));	
		
		tableFindReservation.getTableHeader().setFont(new Font("Tahoma", Font.BOLD, 13));
		
		btnCancelReservation = new JButton("Cancel Reservation");
		btnCancelReservation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				CancelReservation();
				
			}
		});
		btnCancelReservation.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnCancelReservation.setBounds(336, 271, 169, 23);
		findReservationPanel.add(btnCancelReservation);
		
		btnPrintReservation = new JButton("Print Reservation");
		btnPrintReservation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				PrintReservation();
			}
			
		});
		btnPrintReservation.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnPrintReservation.setBounds(346, 293, 151, 23);
		findReservationPanel.add(btnPrintReservation);		
		
		JPanel viewSeatsAvailablePanel = new JPanel();
		cardPanel.add(viewSeatsAvailablePanel, "View Available Seats");
		viewSeatsAvailablePanel.setLayout(null);
		
		JLabel lblFlightInfo = new JLabel("New label");
		lblFlightInfo.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblFlightInfo.setHorizontalAlignment(SwingConstants.CENTER);
		lblFlightInfo.setBounds(10, 50, 831, 27);
		viewSeatsAvailablePanel.add(lblFlightInfo);
		
		JLabel lblViewSeatsAvailable = new JLabel("View Seats available");
		lblViewSeatsAvailable.setHorizontalAlignment(SwingConstants.CENTER);
		lblViewSeatsAvailable.setBounds(10, 11, 831, 28);
		lblViewSeatsAvailable.setFont(new Font("Tahoma", Font.BOLD, 15));
		viewSeatsAvailablePanel.add(lblViewSeatsAvailable);
		
		JLabel lblSeatsAvailable = new JLabel("No. Of Seats Available");
		lblSeatsAvailable.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblSeatsAvailable.setBounds(38, 109, 159, 14);
		viewSeatsAvailablePanel.add(lblSeatsAvailable);
		
		lblNoOfSeats = new JLabel("New label");
		lblNoOfSeats.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblNoOfSeats.setBounds(214, 110, 94, 14);
		viewSeatsAvailablePanel.add(lblNoOfSeats);
		
		JLabel lblSeatsAvailable_1 = new JLabel("Seats Available");
		lblSeatsAvailable_1.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblSeatsAvailable_1.setBounds(38, 159, 146, 14);
		viewSeatsAvailablePanel.add(lblSeatsAvailable_1);
		
		JScrollPane scrollPane_4 = new JScrollPane();
		scrollPane_4.setBounds(214, 160, 459, 91);
		viewSeatsAvailablePanel.add(scrollPane_4);
		
		lblSeatNo = new JLabel("New label");
		scrollPane_4.setViewportView(lblSeatNo);
		lblSeatNo.setVerticalAlignment(SwingConstants.TOP);
		lblSeatNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		lblFlightInfo.setText("Flight " + objFlight.getFlightNumber() + " From " + objFlight.getOrigin() + " To " + objFlight.getDestination());

	}
	

	public JPanel getCardPanel() {
		return cardPanel;
	}
	
	public void showHidePanel(String strName){
		CardLayout cl = (CardLayout)(cardPanel.getLayout());
	    cl.show(cardPanel, strName);
	}
	
	public void makeReservationForPassenger(){
		
		try
		{
			if(validatePassengerData())
			{
				if(objSeat == null)
				{
					JOptionPane.showMessageDialog(null, "Please select seat.");
					return;
					
				}
				objPassenger = new Passenger(textCustomerName.getText(),textEmailId.getText(),formattedTexttextContactNo.getText(),textAddress.getText());
				objRes = new Reservation( Reservation.getNextReservationNo(),objPassenger, objSeat);
				if(objFlight.makeReservation(objRes)){
					JOptionPane.showMessageDialog( null,
				            "Seat reserved successfully. \n\r Reservation No is " + objRes.getReservationNo() + ".");
					
					clearText();

					showSeatPanelDetails();
				}
			}
						
		}
		catch(Exception ex)
		{
		
		}
		finally{
			
		}
	}
	
	public void addPassengerToWaitingList(){
		
		try
		{
			if(validatePassengerData())
			{
				objPassenger = new Passenger(textCustomerName.getText(),textEmailId.getText(),formattedTexttextContactNo.getText(),textAddress.getText());
				if(objFlight.addPassengerToWaitingList(objPassenger)){
					JOptionPane.showMessageDialog( null,
				            "Passenger added to waiting list successfully.");
					
					clearText();

					showSeatPanelDetails();
				}
			}
						
		}
		catch(Exception ex)
		{
		
		}
		finally{
			
		}
	}
	
	public void clearText()
	{
		try
		{
			textCustomerName.setText("");
			textAddress.setText("");
			textEmailId.setText("");
			formattedTexttextContactNo.setText("");
			textCustomerName.requestFocusInWindow();
			objSeat = null;
			
		}
		catch(Exception ex)
		{
			
		}
		finally
		{
			
		}
		
	}
	
	/* 
	 * Validates all input field on form
	 */
	public boolean validatePassengerData()
	{
		if(textCustomerName.getText().length() == 0)
		{
			JOptionPane.showMessageDialog(null, "Please enter Customer Name.");
			textCustomerName.requestFocusInWindow();
			return false;
		}
		
		if(textEmailId.getText().trim().length() == 0)
		{
			JOptionPane.showMessageDialog(null, "Please enter valid Email Id.");
			textEmailId.requestFocusInWindow();
			return false;
		}
		
		if(!isValidEmailAddress(textEmailId.getText()))
		{
			JOptionPane.showMessageDialog(null, "Email Id is not valid.");
			textEmailId.requestFocusInWindow();
			return false;
		}
		if(textAddress.getText().trim().length() == 0)
		{
			JOptionPane.showMessageDialog(null, "Please enter Address.");
			textAddress.requestFocusInWindow();
			return false;
		}
		if(formattedTexttextContactNo.getText().trim().length() == 0)
		{
			JOptionPane.showMessageDialog(null, "Please enter Contact No.");
			formattedTexttextContactNo.requestFocusInWindow();
			return false;
		}
		
		return true;
	}
	
	
	
	/* to view list of reserved and waiting list passenger*/
	public void ViewPassengerList(){
		try
		{
			//Reserved passenger list from reservation list
			String [] ColName = {"Passenger Name","Email Id","Seat No","Reservation No"};
			DefaultTableModel model = new DefaultTableModel(ColName, 0);
			
			ArrayList<Reservation> ReservationList = objFlight.viewReservations();
			if(ReservationList != null){
			Iterator<Reservation> itr = ReservationList.iterator();
			while(itr.hasNext()){
				
			    objRes = itr.next();
				objPassenger = objRes.getPassenger();
				objSeat = objRes.getSeat();
				
				model.addRow(new String[]{objPassenger.getName(),objPassenger.getEmailId(),objSeat.getNumber(),objRes.getReservationNo()});
				
			}
		
			tableReservation.setModel(model);
			setVisible(true);
			}
		
			//Waiting list passengers from waiting list
			String [] Col1Name = {"Passenger Name","Email Id","Contact No"};
			model = new DefaultTableModel(Col1Name, 0);
			ArrayList<Passenger> waitList = objFlight.viewWaitingList();
			if(waitList != null){
			
				Iterator<Passenger> itr = waitList.iterator();
				while(itr.hasNext()){
					
					objPassenger = itr.next();
								
					model.addRow(new String[]{objPassenger.getName(),objPassenger.getEmailId(),objPassenger.getContactNo()});
					
				}
			
				tableWaiting.setModel(model);
				setVisible(true);
				}
		}
		catch(Exception ex)
		{
			
		}
		finally
		{
			objPassenger = null;
			objSeat = null;
			objRes = null;
		}
		
	}
	
	public void FindPassenger()
	{
		try
		{
			if(textEmailId_find.getText().trim().length() == 0 && textReservationNo.getText().trim().length() == 0)
			{
				JOptionPane.showMessageDialog(null, "Please enter valid Email Id Or Reservation No.");
				textEmailId_find.requestFocusInWindow();
				return;
			}
			
			if(textEmailId_find.getText().trim().length() > 0 && !isValidEmailAddress(textEmailId_find.getText()))
			{
				JOptionPane.showMessageDialog(null, "Email Id is not valid.");
				textEmailId_find.requestFocusInWindow();
				return;
			}
			
			String [] ColName = {"Passenger Name","Email Id","Seat No","Reservation No","Status"};
			DefaultTableModel model = new DefaultTableModel(ColName, 0);
			
			objPassenger = new Passenger("", textEmailId_find.getText().trim(), "", "");
			objRes = new Reservation(textReservationNo.getText(), objPassenger, null);
			ArrayList<Reservation> reservationList = objFlight.FindPassengerReservations(objRes);
			ArrayList<Passenger> waitList = objFlight.FindPassengerWaiting(objPassenger);
			
			
			if(reservationList != null){
				Iterator<Reservation> itr = reservationList.iterator();
				while(itr.hasNext()){
					objRes = itr.next();
					objPassenger = objRes.getPassenger();
					objSeat = objRes.getSeat();
			
					model.addRow(new String[]{objPassenger.getName(),objPassenger.getEmailId(),objSeat.getNumber(),objRes.getReservationNo(),"Reserved"});
				}
			}
			
			if(waitList != null){
				Iterator<Passenger> itr = waitList.iterator();
				while(itr.hasNext()){
					objPassenger = itr.next();
					model.addRow(new String[]{objPassenger.getName(),objPassenger.getEmailId(),"--","--","Waiting"});
					}
			}

			tableFindPassenger.setModel(model);
			setVisible(true);
		}
		catch(Exception ex)
		{
			
		}
		finally
		{
			objPassenger = null;
			objSeat = null;
			objRes = null;
		}
	}
	
	public void FindReservation()
	{
		try
		{
			if(textEmailId_res.getText().trim().length() == 0 && textReservationNo_res.getText().trim().length() == 0)
			{
				JOptionPane.showMessageDialog(null, "Please enter valid Email Id Or Reservation No.");
				textEmailId_res.requestFocusInWindow();
				return;
			}
			
			if(textEmailId_res.getText().trim().length() > 0 && !isValidEmailAddress(textEmailId_res.getText()))
			{
				JOptionPane.showMessageDialog(null, "Email Id is not valid.");
				textEmailId_res.requestFocusInWindow();
				return;
			}
			
			String [] ColName = {"Passenger Name","Email Id","Seat No","Reservation No"};
			DefaultTableModel model = new DefaultTableModel(ColName, 0);
					
			objPassenger = new Passenger("", textEmailId_res.getText().trim(), "", "");
			objRes = new Reservation(textReservationNo_res.getText(), objPassenger, null);
			ArrayList<Reservation> reservationList = objFlight.FindPassengerReservations(objRes);
			
			if(reservationList != null){
				Iterator<Reservation> itr = reservationList.iterator();
				while(itr.hasNext()){
					objRes = itr.next();
					objPassenger = objRes.getPassenger();
					objSeat = objRes.getSeat();
			
					model.addRow(new String[]{objPassenger.getName(),objPassenger.getEmailId(),objSeat.getNumber(),objRes.getReservationNo()});
				}
			}
			
			tableFindReservation.setModel(model);
			setVisible(true);
		}
		catch(Exception ex)
		{
			
		}
		finally
		{
			objPassenger = null;
			objSeat = null;
		}
	}
	
	public void CancelReservation()
	{
		if(tableFindReservation.getRowCount()== 1)
		{
			if(JOptionPane.showConfirmDialog(null, "Are you sure you want to cancel this reservation?") == JOptionPane.YES_OPTION){
				if(objFlight.cancelReservation(objRes)){
					JOptionPane.showMessageDialog(null, "Reservation No. " + objRes.getReservationNo() +  " cancelled successfully.");
					
					setControls();
				}
			}
			
			
		}
		else
		{
			JOptionPane.showMessageDialog(null, "No record found");
		}
			
	}
	
	public void PrintReservation(){
		try {
			if(tableFindReservation.getModel().getRowCount() > 0){
				Object [] options = {"Print","Cancel"};
				String message = "Reservation\n\r\n\r" + "\n\rFight " + objFlight.getFlightNumber() + " From " + objFlight.getOrigin() + " To " + objFlight.getDestination() + "\n\r"
				+  "\n\r\n\r Reservation No : " + objRes.getReservationNo() + "\n\r\n\r Passenger Name : " + objRes.getPassenger().getName() 
				+ "\n\r\n\r Email Id : " + objRes.getPassenger().getEmailId() + "\n\r\n\r Seat No : " + objRes.getSeat().getNumber()  + "\n\r\n\r \n\r\n\r ";
				int n = JOptionPane.showOptionDialog(null, message, "Print Reservation", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
				if(n == JOptionPane.YES_OPTION || n ==JOptionPane.NO_OPTION)
				{
					
				}
			}
			//tableFindReservation.print();
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.err.println("Error printing: " + e.getMessage());
			e.printStackTrace();
		}
		
	}
	
	/* Validates if email address is in correct format*/
	public boolean isValidEmailAddress(String emailAddress){  
		   String  expression="^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";  
		   CharSequence inputStr = emailAddress;  
		   Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);  
		   Matcher matcher = pattern.matcher(inputStr);  
		   return matcher.matches();  
		  
		 }  
	public void setSelectedSeatNo(String seatNo)
	{
		//objFlight.getSeatList().add(new Seat(seatNo, true));
		objSeat = new Seat(seatNo, true);
	}
	
	
	
	public void showSeatPanelDetails()
	{
		Passenger objPassDetails;
		for (int idx = 0; idx < objFlight.getMaxrows(); idx++)
		{
			for (int idy = 0; idy < this.objFlight.getMaxSeatPerRow(); idy++)
			{
				if(objFlight.getSeatArray()[idx][idy] == null)
				{
					buttonArray[idx][idy].setBackground(Color.green);
					buttonArray[idx][idy].setEnabled(true);
					buttonArray[idx][idy].setToolTipText("Available");
				}
				else
				{
					buttonArray[idx][idy].setBackground(Color.gray);
					buttonArray[idx][idy].setEnabled(false);
					
					objPassDetails = objFlight.getSeatArray()[idx][idy];
					buttonArray[idx][idy].setToolTipText("Reserved for Passenger : " + objPassDetails.getName());
				}
			}
			
		}
		showHideReserve();
	}
	
	public void showHideReserve()
	{
		if(objFlight.getSeatsLeft() > 0)
		{
			btnResereve.setVisible(true);
			btnAddWaiting.setVisible(false);
		}
		else
		{
			btnResereve.setVisible(false);
			btnAddWaiting.setVisible(true);
		}
	}
	
	public void viewAvailableSeats()
	{
		lblNoOfSeats.setText(Integer.toString(objFlight.getSeatsLeft()));
		
		String availableSeats = "";
		for(String seatNo : objFlight.viewAvailableSeats())
		{
			availableSeats = availableSeats+ seatNo + "," ;
		}
		lblSeatNo.setText(availableSeats);
		
	}
	
	public void setControls()
	{
		textEmailId_res.setText("");
		textReservationNo_res.setText("");
		textEmailId_res.requestFocusInWindow();
		tableReservation.removeAll();
		String [] ColName = {"Passenger Name","Email Id","Seat No","Reservation No"};
		DefaultTableModel model = new DefaultTableModel(ColName, 0);
		
		tableFindReservation.setModel(model);
		setVisible(true);
	}
}
